package com.jms.fu.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.jms.fu.exception.DAOException;
import com.jms.fu.model.bean.App;
import com.jms.fu.model.bo.AppBO;
import com.jms.fu.model.bo.AppDownBO;
import com.jms.fu.model.bo.UserBO;

public class GetAppDetailAction extends Action{
	private static Logger logger = Logger.getLogger(LoginAction.class);

	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int appid = 0;
		try {
			appid = Integer.parseInt(request.getParameter("appid"));
		} catch (NumberFormatException e) {
			logger.error(e.getMessage());
		}
		HttpSession session = request.getSession();
		String username = (String)session.getAttribute("username");
		String role = (String)session.getAttribute("role");
		AppBO appbo = new AppBO();
		
		try {
			App temp = appbo.getAppByID(appid);
			ArrayList<String> listf = appbo.getNumberOfFile(getServlet().getServletContext().getRealPath("/")+temp.getLink()+"/Images");
			String imagecontent="";
			int num = listf.size();
			for(int i = 0;i<num/4+1;i++){
				if(num==0){
					imagecontent="";
					break;
				}
				String a="";
				if(i==0){
					a = " active";
				}
				imagecontent +="<div class='item"+a+"'> <div class='row'>";
					for(int j=0;j<((!(num%3==0)&&i==num/4)?(num%3):3);j++){
						imagecontent += "<div class='col-md-4'>"
								+ "<a class='thumbnail' href='#'><img alt='' src='"+temp.getLink()+"/Images/"+listf.get(i*3+j)+"'></a>"
								+ "</div>";
					}
				imagecontent += "</div>"
						+ "</div>"; 
			}
			
			ArrayList<String> avatar = appbo.getNumberOfFile(getServlet().getServletContext().getRealPath("/")+temp.getLink()+"/Avatar");
			String avatarLink;
			if(avatar.size()==0){
				avatarLink = "references/images/avatar.jpg"; 
			}else{
				avatarLink = ""+temp.getLink()+"/Avatar/"+avatar.get(0);
			}
			UserBO userbo = new UserBO();
			AppDownBO appdown = new AppDownBO();
			boolean checkGetLink = false;
			String linkDownload = "";
			if(role!=null||username!=null){
				if (temp.getPrice() == 0 || appdown.checkAppOwnByUsername(username, appid)
						|| temp.getDevId().equals(username)
						|| role.equals("Administrator")) {
					String appname = appbo.getNumberOfFile(
							getServlet().getServletContext().getRealPath("/")
									+ temp.getLink()).get(0);
					linkDownload = temp.getLink() + "/" + appname;
					checkGetLink=true;
				}
			}
			
			String result = "{\"title\" : \""
					+ "<img style='width:120px;height:120px' src='"+avatarLink+"'>"
					+ "<h3>"+temp.getAppName()+"</h3>"
					+ "<a href='viewUser.jsm?usernameCheck="+temp.getDevId()+"&roleCheck="+(userbo.getUserInfoByUsername(temp.getDevId()).getRole()==0?1:userbo.getUserInfoByUsername(temp.getDevId()).getRole())+"'>"+temp.getDevId()+"</a>\","
					+ "\"infor\" : \"<ul class='meta-search'>"
							+ "<li><i class='glyphicon glyphicon-calendar'></i> <span>"+temp.getDateUpload()+"</span></li>"
							+ "<li><i class='glyphicon glyphicon-time'></i> <span>Version: "+temp.getUpVersion()+"</span></li>"
							+ "<li><i class='glyphicon glyphicon-asterisk'></i> <span>Requirement OS : "+temp.getRequireOS()+"</span></li>"
							+ "<li><i class='glyphicon glyphicon-eye-open'></i> <span>Number of download: "+temp.getNumberOfDownload()+" </span></li>"
							+ "<li><i class='glyphicon glyphicon-briefcase'></i> <span>Size: "+temp.getSize()+"</span></li>"
							+ "<li><i class='glyphicon glyphicon-download-alt'></i> "							
							+(!checkGetLink? ("<a class='btn btn-default' id='btn-buy-"+appid+"' onclick='buyItem("+temp.getAppId()+")' style=' padding: 0px 5px 0px 5px; font-size: 10px;  border-radius: 15px;'>"+(temp.getPrice()>0?temp.getPrice()+" $":"Free")+"</a>"):
								"<a class='btn btn-default' href='"+linkDownload+"' id='btn-buy-"+appid+"' style=' padding: 0px 5px 0px 5px; font-size: 10px;  border-radius: 15px;'>"+"Download"+"</a>")
							+ "</li></ul>\","
							+ "\"imagecontent\" : \""+imagecontent+"\","
							+ "\"description\" : \""+	temp.getDescription()+ "\"}";
			
			PrintWriter out = response.getWriter();
			out.print(result);
			out.close();
			return null;
		} catch (DAOException | IOException e) {
			logger.error(e.getMessage());
			return mapping.findForward("failed");
		}
	}
}
